/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author heguanghua
 * @since 2018/10/18
 */
var form = null;

layui.define(['form', 'layedit', 'laydate'], function (exports) {
    let $ = layui.$;
    form = layui.form;
    let layer = layui.layer;
    let laydate = layui.laydate;
    let index;

    let contractPaymentRecordVO = json.contractPaymentRecordVO;

    let agreedPaymentDay = {
        elem: '#agreedPaymentDay',
        type: 'date',
    };
    if (contractPaymentRecordVO.agreedPaymentDay != null) {
        agreedPaymentDay.value = new Date(contractPaymentRecordVO.agreedPaymentDay);
    } else {
        agreedPaymentDay.value = new Date();
    }
    laydate.render(agreedPaymentDay);

    let actualPaymentDay = {
        elem: '#actualPaymentDay',
        type: 'date',
    };
    if (contractPaymentRecordVO.actualPaymentDay != null) {
        actualPaymentDay.value = new Date(contractPaymentRecordVO.actualPaymentDay);
    } else {
        actualPaymentDay.value = new Date();
    }
    laydate.render(actualPaymentDay);

    form.verify({
        bigAmount: function (value) {
            if (value !== "") {
                let r = /^([1-9]\d{0,9}|0)([.]?|(\.\d{1,2})?)$/;
                if (!r.test(value)) {
                    return '格式内容不正确，请重新输入';
                }
                if (Number(value) > 100000000) {
                    return '数值不能超过100000000.00';
                }
            }
        }
    });

    zmnLayUtil.layFormVerify(form);

    form.render(null, "zmn-form");

    // 提交
    form.on('submit(zmn-form-submit)', function (contractPaymentRecordVO) {
        let data = contractPaymentRecordVO.field;
        data['amount'] = data.amount * 100;
        let url = "/contract/payment/record/addEdit.action";
        var openId = layer.open({type: 3});
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success:function(result) {
                layer.close(openId);
                if(result.status == 200) {
                    layer.msg(result.message,{time: 1000},()=>{
                        zmnLayUtil.closeThisIframe(parent);
                        parent.location.reload();
                    });
                } else {
                    layer.msg(result.message, {"icon": 2});
                }
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                layer.close(openId);
            }
        });

        return false;
    });

    //点击取消
    $(document).on('click', '#cancel', function () {
        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index);
    });

    //对外暴露的接口
    exports('contract_payment_record_detail_edit_js', {});
});

